﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class CountryManagerForm : Form
    {
        private DataTable _dtCountry;
        private bool _isAddStatus = false;
        
        public CountryManagerForm()
        {
            InitializeComponent();
        }

        private void CountryManagerForm_Load(object sender, EventArgs e)
        {
            InitValidationRules();
            if (Thread.CurrentPrincipal.IsInRole("基础信息管理员") == true)
            {
                btnCancel.Enabled = true;
                btnSave.Enabled = true;
                btnDelete.Enabled = true;
                btnAdd.Enabled = true;
                btnModify.Enabled = true;
            }
            else
            {
                btnCancel.Enabled = false;
                btnSave.Enabled = false;
                btnDelete.Enabled = false;
                btnAdd.Enabled = false;
                btnModify.Enabled = false;

            }
        }
        private void InitUI()
        {
            tbCountryName.DataBindings.Clear();
            tbFrenchName.DataBindings.Clear();
            tbEnglishName.DataBindings.Clear();
            tbCountryCode.DataBindings.Clear();
            tbOrderID.DataBindings.Clear();

            tbCountryName.DataBindings.Add("EditValue", _dtCountry, "CountryName");
            tbEnglishName.DataBindings.Add("EditValue", _dtCountry, "EnglishName");
            tbFrenchName.DataBindings.Add("EditValue", _dtCountry, "FrenchName");
            tbCountryCode.DataBindings.Add("EditValue", _dtCountry, "CountryCode");
            tbOrderID.DataBindings.Add("EditValue", _dtCountry, "OrderID");
        }

        #region 按钮事件处理

        private void btnLoad_Click(object sender, EventArgs e)
        {
            _dtCountry = BaseInfoCtrl.GetCountryData();
            this.countryGridControl.DataSource = _dtCountry;

            InitUI();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            countryGridView.AddNewRow();

            _isAddStatus = true;
            SetUIElementStatus(true);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (_dtCountry == null) return;

            _isAddStatus = false;

            SetUIElementStatus(true);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider.Validate()) return;

            try
            {
                CountryEntity entity = new CountryEntity();
                entity.CountryName = tbCountryName.Text;
                entity.EnglishName = tbEnglishName.Text;
                entity.FrenchName = tbFrenchName.Text;
                entity.CountryCode = tbCountryCode.Text;
                entity.OrderID = tbOrderID.Text == "" ? 0 : int.Parse(tbOrderID.Text);

                if (_isAddStatus)
                {
                    string countryId = BaseInfoCtrl.CreateCountryData(entity); ;
                    countryGridView.SetRowCellValue(countryGridView.FocusedRowHandle, "CountryID", countryId);
                }
                else
                {
                    entity.CountryID = int.Parse(countryGridView.GetRowCellValue(countryGridView.FocusedRowHandle, "CountryID").ToString());
                    BaseInfoCtrl.UpdateCountryData(entity);
                }
                MessageBox.Show("保存成功！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存过程出现错误:" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                _isAddStatus = false;
                SetUIElementStatus(false);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnLoad.PerformClick();

            SetUIElementStatus(false);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (countryGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确定删除选中的国家信息吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string countryId = countryGridView.GetRowCellValue(countryGridView.FocusedRowHandle, "CountryID").ToString();

                countryGridView.DeleteRow(countryGridView.FocusedRowHandle);
                BaseInfoCtrl.DeleteCountryData(countryId);
            }

        }
        #endregion

        /// <summary>
        /// 控件数据校验
        /// </summary>
        private void InitValidationRules()
        {
            // <notEmptyTextEdit> 
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "不能为空";
            // </notEmptyTextEdit>

            //不能为空
            dxValidationProvider.SetValidationRule(tbCountryName, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbEnglishName, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbCountryCode, notEmptyValidationRule);

        }


        /// <summary>
        /// 设定界面元素状态
        /// </summary>
        /// <param name="bModifyOrAddMode">设置当前状态为修改或添加状态</param>
        private void SetUIElementStatus(bool bSetToModifyOrAddStatus)
        {
            if (bSetToModifyOrAddStatus == true)
            {
                btnCancel.Visible = true;
                btnSave.Visible = true;
                countryGroupPane.Enabled = true;
                countryGridControl.Enabled = false;
            }
            else
            {
                btnCancel.Visible = false;
                btnSave.Visible = false;
                countryGroupPane.Enabled = false;
                countryGridControl.Enabled = true;
            }
        }
    }
}
